/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.sqlServer.PpPallet;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.repository.PpPalletRepository;
import me.zhengjie.service.PpPalletService;
import me.zhengjie.service.dto.PpPalletDto;
import me.zhengjie.service.dto.PpPalletQueryCriteria;
import me.zhengjie.service.mapper.PpPalletMapper;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-16
**/
@Service
@RequiredArgsConstructor
public class PpPalletServiceImpl implements PpPalletService {

    private final PpPalletRepository ppPalletRepository;
    private final PpPalletMapper ppPalletMapper;

    @Override
    public PageResult<PpPalletDto> queryAll(PpPalletQueryCriteria criteria, Pageable pageable){
        Page<PpPallet> page = ppPalletRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppPalletMapper::toDto));
    }

    @Override
    public List<PpPalletDto> queryAll(PpPalletQueryCriteria criteria){
        return ppPalletMapper.toDto(ppPalletRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PpPalletDto findById(Integer id) {
        PpPallet ppPallet = ppPalletRepository.findById(id).orElseGet(PpPallet::new);
        ValidationUtil.isNull(ppPallet.getId(),"PpPallet","id",id);
        return ppPalletMapper.toDto(ppPallet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PpPallet resources) {
        if(ppPalletRepository.findByPalletCode(resources.getPalletCode()) != null){
            throw new EntityExistException(PpPallet.class,"pallet_code",resources.getPalletCode());
        }
        ppPalletRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PpPallet resources) {
        PpPallet ppPallet = ppPalletRepository.findById(resources.getId()).orElseGet(PpPallet::new);
        ValidationUtil.isNull( ppPallet.getId(),"PpPallet","id",resources.getId());
        PpPallet ppPallet1 = null;
        ppPallet1 = ppPalletRepository.findByPalletCode(resources.getPalletCode());
        if(ppPallet1 != null && !ppPallet1.getId().equals(ppPallet.getId())){
            throw new EntityExistException(PpPallet.class,"pallet_code",resources.getPalletCode());
        }
        ppPallet.copy(resources);
        ppPalletRepository.save(ppPallet);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppPalletRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PpPalletDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PpPalletDto ppPallet : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("载盘编码", ppPallet.getPalletCode());
            map.put("pp批次", ppPallet.getPpBatch());
            map.put("载盘尺寸", ppPallet.getPalletSize());
            map.put(" palletLocation",  ppPallet.getPalletLocation());
            map.put("创建时间", ppPallet.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PpPallet findByPalletCode(String palletCode) {
        return ppPalletRepository.findByPalletCode(palletCode);
    }
}